﻿using AiXiu.Common;
using AiXiu.IService;
using AiXiu.Model;
using AiXiu.Service;
using CaptchaGen;
using System.Collections.Generic;
using System.Web.Mvc;

namespace AiXiu.WebSite.Controllers
{
    public class HomeController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {
            IVideoService videoService = new VideoService();
            List<TBVideos> list = videoService.GetVideos();
            return View(list);
        }

        public ActionResult Register()
        {
            return View();
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult GetVerCode()
        {
            //1、生成验证码的文字
            string code = CommonHelper.CreateVerifyCode(4);
            //2、临时保存code,登录时需要对比验证
            TempData["VerCode"] = code;
            //3、调用ImageFactory.GenerateImage(验证码文字,宽度,高度,字体大小,扭曲程度(越大越扭曲))
            var img = ImageFactory.GenerateImage(code, 60, 100, 15, 6);
            //4、返回文件File(src, type)
            return File(img, "imgge/jpeg");
        }

        [HttpPost]
        public ActionResult SubmitRegister(TBLogins login,string verCode)
        {
            // 验证码验证
            if (TempData["VerCode"]==null)
            {
                return Json(OperResult.Failed("验证码已失效"));
            }
            if (TempData["VerCode"].ToString()!=verCode)
            {
                return Json(OperResult.Failed("验证码有误"));
            }

            // 注册用户
            IUserService userService = new UserService();
            OperResult result = userService.Register(login);
            if (result.StatusCode == StatusCode.Succeed)
            {
                return Json(OperResult.Succeed());
            }
            else
            {
                return Json(OperResult.Failed(result.Message));
            }
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitLogin(string userName,string password)
        {
            IUserService userService = new UserService();
            OperResult<TBUsers> operResult = userService.LoginByUserName(userName,password);
            if (operResult.StatusCode == StatusCode.Succeed)
            {
                //登录成功，缓存用户资料
                TBUsers user = operResult.ReadResult();
                IdentityManager.SaveUser(user);
                return Json(OperResult.Succeed());
            }
            else
            {
                return Json(OperResult.Failed(operResult.Message));
            }
        }
        
        public ActionResult Mobile()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitMobile(string mobile, string password)
        {
            IUserService userService = new UserService();
            OperResult<TBUsers> operResult = userService.LoginByMobile(mobile, password);
            if (operResult.StatusCode == StatusCode.Succeed)
            {
                //登录成功，缓存用户资料
                TBUsers user = operResult.ReadResult();
                IdentityManager.SaveUser(user);
                return Json(OperResult.Succeed());
            }
            else
            {
                return Json(OperResult.Failed(operResult.Message));
            }
        }
    }
}